﻿






--删除（Delete）操作
CREATE PROCEDURE [dbo].[RDIFramework_PiUser_DELETE]
(	@id int	 = null  
   ,@ModifiedOn datetime = NULL
   ,@ModifyUserId nvarchar(50)
   ,@ModifiedBy nvarchar(50)
   ,@OperationType nvarchar(30)				--DELETE:物理删除数据、SETDELMARK:逻辑删除数据
   ,@SuccessMark INT = 0 OUTPUT		        --成功标志 >0：操作成功 <=0,操作失败
   ,@ReturnInfo nvarchar(500) = '' OUTPUT   --操作提示信息 
)
AS
BEGIN
	SET NOCOUNT ON
	IF @OperationType IS NULL
	BEGIN
		RAISERROR ( '操作类型不能为空！', 16, 1 )
		SELECT @ReturnInfo  =  '操作类型不能为空！'
		SELECT @SuccessMark = 0
		ROLLBACK TRAN
		RETURN
	END
	
	IF @OperationType IN('DELETE','SETDELMARK')
	BEGIN
		IF @Id IS NULL
		BEGIN
			RAISERROR ( '用户主键为空！', 16, 1 )
			SELECT @ReturnInfo =  '用户主键为空！'
			SELECT @SuccessMark = 0
			ROLLBACK TRAN
		END
	END
	
	--1、物理删除指定数据
	IF @OperationType = 'DELETE' --物理删除数据
	BEGIN
		BEGIN TRAN
		DELETE FROM dbo.PiUser
		WHERE Id = @Id		
		IF @@ROWCOUNT > 0 AND @@ERROR=0    
	    BEGIN 
			SELECT @ReturnInfo='成功删除用户数据！'   
			SELECT @SuccessMark = 1   
			COMMIT TRAN
			RETURN
	    END    
		ELSE
		BEGIN
			SELECT @ReturnInfo='删除用户数据失败！'
			SELECT @SuccessMark = 0
			ROLLBACK TRAN
			RETURN
		END 
	END
	
	--2、逻辑删除指定数据
	IF @OperationType = 'SETDELMARK' --逻辑删除数据[置删除标记]
	BEGIN		
		BEGIN TRAN
		--DECLARE @DELETEROWCOUNT INT = 0
		IF @ModifyUserId IS NULL OR @ModifiedBy IS NULL
		BEGIN
			SELECT @ModifyUserId = ModifyUserId,@ModifiedBy = ModifiedBy FROM dbo.PiUser
			WHERE Id = @Id
		END
		
		UPDATE dbo.PiUser
		SET DeleteMark = 1,ModifiedOn = GETDATE(),ModifyUserId = @ModifyUserId,ModifiedBy = @ModifiedBy
		WHERE Id = @Id
		
		IF @@ROWCOUNT > 0 AND @@ERROR=0    
	    BEGIN 
			SELECT @ReturnInfo='成功删除用户数据！'  
			SELECT @SuccessMark = 1    
			COMMIT TRAN
			RETURN
	    END    
		ELSE
		BEGIN
			SELECT @ReturnInfo='删除用户数据失败！'
			SELECT @SuccessMark = 0
			ROLLBACK TRAN
			RETURN
		END 		
	END
END








